package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 自定义元数据对象处理器
 * @author GRS
 * @since 2024/4/19 下午3:51
 */
@Component
@Slf4j
public class CustomMetaObjectHandler implements MetaObjectHandler
{
    private static final SimpleDateFormat SDF =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 插入操作，自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject)
    {
        metaObject.setValue("createTime", SDF.format(System.currentTimeMillis()));
        metaObject.setValue("updateTime", SDF.format(System.currentTimeMillis()));

        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }

    /**
     * 更新操作，自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject)
    {
        metaObject.setValue("updateTime", SDF.format(System.currentTimeMillis()));

        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}